package com.integral.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.integral.biz.IUserBiz;
import com.integral.entity.UserInfo;
import com.integral.util.StringUtils;

@Controller
public class LoginController {

	@Resource
	private IUserBiz userBiz;
	
	/**
	 * 用户登录
	 * @param userInfo
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/login")
	public String login(){
		return "index";
	}
	
	@RequestMapping(value="/loginUser")
	public String loginUser(UserInfo userInfo,HttpServletRequest request, HttpServletResponse response){
		//当前没有登录用户，或者登录账号为空，则跳转到登录页面
		if(null == userInfo || StringUtils.isEmpty(userInfo.getUserCode())){
			return "index";
		}
		//判断当前登录用户是否正确
		UserInfo user = userBiz.findUserInfo(userInfo);
		//用户不存在,跳转到登录页面
		if(null == user){
			return "index";
		}
		
		HttpSession session = request.getSession();
		//把用户对象放入session
		session.setAttribute("userInfo", user);
		session.setAttribute("userType", user.getUserType());
		//把用户名字存入attribute，用于页面展示
		request.setAttribute("userName", user.getUserName());
		return "management/forward";
	}
}
